'''
Created on 16/09/2011

@author: uqjliu10
'''
from string import Template
from urllib2 import urlopen, URLError, HTTPError
import simplejson
import time

"""
sample url
http://www.panoramio.com/map/get_panoramas.php?set=public&from=0&to=20&minx=121.07&miny=30.46&maxx=121.60&maxy=31.22&size=medium&mapfilter=false
sample json
{"count": 2828, "photos": [{"upload_date": "05 April 2008", "owner_name": "\u92ad\u5f62\u8b66\u90e8", "photo_id": 9134590, "longitude": 121.42261500000001, "height": 375, "width": 500, "photo_title": "\u4e0a\u6d77\u5357\u99c5\u3000CRH", "latitude": 31.153690999999998, "owner_url": "http://www.panoramio.com/user/771061", "owner_id": 771061, "photo_file_url": "http://mw2.google.com/mw-panoramio/photos/medium/9134590.jpg", "photo_url": "http://www.panoramio.com/photo/9134590"}, {"upload_date": "08 March 2008", "owner_name": "Baycrest", "photo_id": 8377125, "longitude": 121.57983, "height": 375, "width": 500, "photo_title": "\u5f20\u6c5f\u9ad8\u79d1\u7ad9(\u8001\u7ad9)\u9ad8\u67b6\u8def\u8f68 Line 2 Elevated Railway Track viewed from Zhangjiang High-Tech Park Station (Old Station)", "latitude": 31.203240000000001, "owner_url": "http://www.panoramio.com/user/239280", "owner_id": 239280, "photo_file_url": "http://mw2.google.com/mw-panoramio/photos/medium/8377125.jpg", "photo_url": "http://www.panoramio.com/photo/8377125"}, {"upload_date": "24 March 2008", "owner_name": "Baycrest", "photo_id": 8806420, "longitude": 121.519201, "height": 375, "width": 500, "photo_title": "\u4e0a\u6d77\u5bb6\u4e50\u798f\u65b0\u91cc\u57ce\u5e97 Carrefour in Shanghai", "latitude": 31.162523, "owner_url": "http://www.panoramio.com/user/239280", "owner_id": 239280, "photo_file_url": "http://mw2.google.com/mw-panoramio/photos/medium/8806420.jpg", "photo_url": "http://www.panoramio.com/photo/8806420"}, {"upload_date": "03 February 2011", "owner_name": "Faintlightofdawn", "photo_id": 47536950, "longitude": 121.472236, "height": 295, "width": 500, "photo_title": "The Seed Cathedral, UK - \u82f1\u56fd\u9986, \u79cd\u5b50\u5723\u6bbf", "latitude": 31.186150999999999, "owner_url": "http://www.panoramio.com/user/1235840", "owner_id": 1235840, "photo_file_url": "http://mw2.google.com/mw-panoramio/photos/medium/47536950.jpg", "photo_url": "http://www.panoramio.com/photo/47536950"}, {"upload_date": "19 April 2011", "owner_name": "Pozlp\u98ce\u683c", "photo_id": 51306300, "longitude": 121.40285299999999, "height": 333, "width": 500, "photo_title": "\u8679\u6865\u7ecf\u8d38\u5f00\u53d1\u533a Hongqiao economics and trade development zone ", "latitude": 31.203386999999999, "owner_url": "http://www.panoramio.com/user/4826506", "owner_id": 4826506, "photo_file_url": "http://mw2.google.com/mw-panoramio/photos/medium/51306300.jpg", "photo_url": "http://www.panoramio.com/photo/51306300"}, {"upload_date": "04 March 2007", "owner_name": "Baycrest", "photo_id": 1154142, "longitude": 121.364733, "height": 375, "width": 500, "photo_title": "\u95f5\u884c\u5f00\u53d1\u533a\u7ad9 5\u53f7\u7ebf\u7ad9\u53f0 Line 5 Platform of Minhang Development Zone Station", "latitude": 31.002459999999999, "owner_url": "http://www.panoramio.com/user/239280", "owner_id": 239280, "photo_file_url": "http://mw2.google.com/mw-panoramio/photos/medium/1154142.jpg", "photo_url": "http://www.panoramio.com/photo/1154142"}, {"upload_date": "17 May 2008", "owner_name": "GAST1", "photo_id": 10327642, "longitude": 121.46980600000001, "height": 500, "width": 375, "photo_title": "Ponte Xupu e tifone Matsu", "latitude": 31.131364000000001, "owner_url": "http://www.panoramio.com/user/1630174", "owner_id": 1630174, "photo_file_url": "http://mw2.google.com/mw-panoramio/photos/medium/10327642.jpg", "photo_url": "http://www.panoramio.com/photo/10327642"}, {"upload_date": "19 February 2009", "owner_name": "\u01a4\u014d\u019d\u0193", "photo_id": 19147175, "longitude": 121.44752499999998, "height": 375, "width": 500, "photo_title": "\u4e0a\u6d77\u56fd\u9645\u793c\u62dc\u5802", "latitude": 31.206834000000001, "owner_url": "http://www.panoramio.com/user/2725589", "owner_id": 2725589, "photo_file_url": "http://mw2.google.com/mw-panoramio/photos/medium/19147175.jpg", "photo_url": "http://www.panoramio.com/photo/19147175"}, {"upload_date": "19 September 2007", "owner_name": "\u5409\u6613", "photo_id": 4751931, "longitude": 121.560287, "height": 333, "width": 500, "photo_title": "\u4e0a\u6d77\u65b0\u570b\u969b\u535a\u89bd\u4e2d\u5fc3\u2014\u2014\u5409\u6613", "latitude": 31.20825, "owner_url": "http://www.panoramio.com/user/604433", "owner_id": 604433, "photo_file_url": "http://mw2.google.com/mw-panoramio/photos/medium/4751931.jpg", "photo_url": "http://www.panoramio.com/photo/4751931"}, {"upload_date": "20 March 2008", "owner_name": "Baycrest", "photo_id": 8678444, "longitude": 121.41226699999999, "height": 375, "width": 500, "photo_title": "\u5ef6\u5b89\u897f\u8def\u7ad9\u8def\u8f68 Shmetro Tracks at Yanan Road (W) Station", "latitude": 31.210595000000001, "owner_url": "http://www.panoramio.com/user/239280", "owner_id": 239280, "photo_file_url": "http://mw2.google.com/mw-panoramio/photos/medium/8678444.jpg", "photo_url": "http://www.panoramio.com/photo/8678444"}, {"upload_date": "07 March 2007", "owner_name": "Frank Kuo", "photo_id": 1205081, "longitude": 121.50501300000001, "height": 375, "width": 500, "photo_title": "\u4e0a\u6d77---\u9ec3\u6d66\u6c5f\u591c\u666f", "latitude": 31.216325000000001, "owner_url": "http://www.panoramio.com/user/159100", "owner_id": 159100, "photo_file_url": "http://mw2.google.com/mw-panoramio/photos/medium/1205081.jpg", "photo_url": "http://www.panoramio.com/photo/1205081"}, {"upload_date": "07 May 2008", "owner_name": "Baycrest", "photo_id": 9999981, "longitude": 121.57452499999999, "height": 375, "width": 500, "photo_title": "\u7f57\u5c71\u8def\u4e0a\u7684\u78c1\u6d6e\u5217\u8f66\u8f68\u9053 Maglev Train Tracks at Luoshan Road", "latitude": 31.195823000000001, "owner_url": "http://www.panoramio.com/user/239280", "owner_id": 239280, "photo_file_url": "http://mw2.google.com/mw-panoramio/photos/medium/9999981.jpg", "photo_url": "http://www.panoramio.com/photo/9999981"}, {"upload_date": "04 March 2007", "owner_name": "guhongyu", "photo_id": 1162014, "longitude": 121.436949, "height": 385, "width": 500, "photo_title": "\u4e0a\u6d77\u516b\u4e07\u4eba\u4f53\u80b2\u9986", "latitude": 31.183361000000001, "owner_url": "http://www.panoramio.com/user/33223", "owner_id": 33223, "photo_file_url": "http://mw2.google.com/mw-panoramio/photos/medium/1162014.jpg", "photo_url": "http://www.panoramio.com/photo/1162014"}, {"upload_date": "07 June 2008", "owner_name": "Baycrest", "photo_id": 10988881, "longitude": 121.51207700000001, "height": 375, "width": 500, "photo_title": "\u4e34\u6c82\u65b0\u6751\u7ad9 Linyi Xincun Station", "latitude": 31.195104000000001, "owner_url": "http://www.panoramio.com/user/239280", "owner_id": 239280, "photo_file_url": "http://mw2.google.com/mw-panoramio/photos/medium/10988881.jpg", "photo_url": "http://www.panoramio.com/photo/10988881"}, {"upload_date": "31 July 2009", "owner_name": "S_Mori", "photo_id": 25110753, "longitude": 121.49805499999999, "height": 375, "width": 500, "photo_title": "Nanpu Bridge", "latitude": 31.209607999999999, "owner_url": "http://www.panoramio.com/user/30202", "owner_id": 30202, "photo_file_url": "http://mw2.google.com/mw-panoramio/photos/medium/25110753.jpg", "photo_url": "http://www.panoramio.com/photo/25110753"}, {"upload_date": "04 March 2007", "owner_name": "Baycrest", "photo_id": 1154170, "longitude": 121.432732, "height": 375, "width": 500, "photo_title": "\u4e0a\u6d77\u4f53\u80b2\u9986\u7ad9 4\u53f7\u7ebf\u5927\u5802 Line 4 Concourse Shanghai Indoor Stadium Station", "latitude": 31.184664000000001, "owner_url": "http://www.panoramio.com/user/239280", "owner_id": 239280, "photo_file_url": "http://mw2.google.com/mw-panoramio/photos/medium/1154170.jpg", "photo_url": "http://www.panoramio.com/photo/1154170"}, {"upload_date": "05 April 2008", "owner_name": "Baycrest", "photo_id": 9115224, "longitude": 121.493099, "height": 500, "width": 375, "photo_title": "\u4f4d\u4e8e\u5c0f\u5357\u95e8\u7684\u706b\u8b66\u949f\u697c Fire Alarm Tower at Xiaonanmen", "latitude": 31.217931, "owner_url": "http://www.panoramio.com/user/239280", "owner_id": 239280, "photo_file_url": "http://mw2.google.com/mw-panoramio/photos/medium/9115224.jpg", "photo_url": "http://www.panoramio.com/photo/9115224"}, {"upload_date": "15 September 2007", "owner_name": "Jeff Metal", "photo_id": 4659090, "longitude": 121.421435, "height": 500, "width": 333, "photo_title": "Shanghai Back Street 1", "latitude": 31.211791999999999, "owner_url": "http://www.panoramio.com/user/66666", "owner_id": 66666, "photo_file_url": "http://mw2.google.com/mw-panoramio/photos/medium/4659090.jpg", "photo_url": "http://www.panoramio.com/photo/4659090"}, {"upload_date": "01 December 2008", "owner_name": "Baycrest", "photo_id": 16475853, "longitude": 121.41601700000001, "height": 375, "width": 500, "photo_title": "\u8679\u6865\u8def\u7ad9 3,4\u53f7\u7ebf\u5171\u7ebf\u7ad9\u53f0 Line 3 & 4 Platform of Hongqiao Road Station", "latitude": 31.199453999999999, "owner_url": "http://www.panoramio.com/user/239280", "owner_id": 239280, "photo_file_url": "http://mw2.google.com/mw-panoramio/photos/medium/16475853.jpg", "photo_url": "http://www.panoramio.com/photo/16475853"}, {"upload_date": "04 March 2007", "owner_name": "Baycrest", "photo_id": 1154206, "longitude": 121.523724, "height": 375, "width": 500, "photo_title": "\u84dd\u6751\u8def\u7ad9 4\u53f7\u7ebf\u5927\u5802(\u5317) Line 4 North Concourse of Lancun Road Station", "latitude": 31.214224000000002, "owner_url": "http://www.panoramio.com/user/239280", "owner_id": 239280, "photo_file_url": "http://mw2.google.com/mw-panoramio/photos/medium/1154206.jpg", "photo_url": "http://www.panoramio.com/photo/1154206"}, {"upload_date": "07 May 2008", "owner_name": "Baycrest", "photo_id": 9999938, "longitude": 121.489903, "height": 375, "width": 500, "photo_title": "\u8000\u534e\u8def\u7ad9 8\u53f7\u7ebf\u7ad9\u53f0 Line 8 Platform of Yaohua Road Station", "latitude": 31.179107999999999, "owner_url": "http://www.panoramio.com/user/239280", "owner_id": 239280, "photo_file_url": "http://mw2.google.com/mw-panoramio/photos/medium/9999938.jpg", "photo_url": "http://www.panoramio.com/photo/9999938"}, {"upload_date": "04 March 2007", "owner_name": "Baycrest", "photo_id": 1153966, "longitude": 121.43327999999997, "height": 375, "width": 500, "photo_title": "\u6f15\u6eaa\u8def\u7ad9 3\u53f7\u7ebf\u7ad9\u53f0 \u9f99\u6f15\u8def\u7ad9\u65b9\u5411 Line 3 Platform, Caoxi Road Station", "latitude": 31.178868000000001, "owner_url": "http://www.panoramio.com/user/239280", "owner_id": 239280, "photo_file_url": "http://mw2.google.com/mw-panoramio/photos/medium/1153966.jpg", "photo_url": "http://www.panoramio.com/photo/1153966"}, {"upload_date": "04 April 2008", "owner_name": "Baycrest", "photo_id": 9089513, "longitude": 121.49015300000001, "height": 375, "width": 500, "photo_title": "\u7075\u5ca9\u5357\u8def\u7ad9 6\u53f7\u7ebf\u7ad9\u53f0 Line 6 Platform of Lingyan Road (S) Station", "latitude": 31.150687999999999, "owner_url": "http://www.panoramio.com/user/239280", "owner_id": 239280, "photo_file_url": "http://mw2.google.com/mw-panoramio/photos/medium/9089513.jpg", "photo_url": "http://www.panoramio.com/photo/9089513"}, {"upload_date": "31 March 2008", "owner_name": "Baycrest", "photo_id": 8999968, "longitude": 121.22647000000001, "height": 375, "width": 500, "photo_title": "\u677e\u6c5f\u65b0\u57ce\u7ad9 9\u53f7\u7ebf\u7ad9\u53f0 Line 9 Platform of Songjiang Xincheng Station", "latitude": 31.031700000000001, "owner_url": "http://www.panoramio.com/user/239280", "owner_id": 239280, "photo_file_url": "http://mw2.google.com/mw-panoramio/photos/medium/8999968.jpg", "photo_url": "http://www.panoramio.com/photo/8999968"}, {"upload_date": "10 April 2007", "owner_name": "Baycrest", "photo_id": 1719648, "longitude": 121.370248, "height": 375, "width": 500, "photo_title": "\u5317\u65b0\u6cfe\u7ad9 2\u53f7\u7ebf\u7ad9\u53f0 Line 2 Platform of Beixinjing Road Station", "latitude": 31.218288000000001, "owner_url": "http://www.panoramio.com/user/239280", "owner_id": 239280, "photo_file_url": "http://mw2.google.com/mw-panoramio/photos/medium/1719648.jpg", "photo_url": "http://www.panoramio.com/photo/1719648"}, {"upload_date": "20 March 2008", "owner_name": "Baycrest", "photo_id": 8678447, "longitude": 121.49128, "height": 375, "width": 500, "photo_title": "\u4e0a\u5357\u8def \u6210\u5c71\u8def \u4e0a\u5357\u65b0\u6751 Shangnan Xinchun", "latitude": 31.173466000000001, "owner_url": "http://www.panoramio.com/user/239280", "owner_id": 239280, "photo_file_url": "http://mw2.google.com/mw-panoramio/photos/medium/8678447.jpg", "photo_url": "http://www.panoramio.com/photo/8678447"}, {"upload_date": "03 April 2008", "owner_name": "Frank_Richards", "photo_id": 9079578, "longitude": 121.48864, "height": 332, "width": 500, "photo_title": "View from Market", "latitude": 31.211407000000001, "owner_url": "http://www.panoramio.com/user/125747", "owner_id": 125747, "photo_file_url": "http://mw2.google.com/mw-panoramio/photos/medium/9079578.jpg", "photo_url": "http://www.panoramio.com/photo/9079578"}, {"upload_date": "03 February 2008", "owner_name": "23ad.cn", "photo_id": 7565141, "longitude": 121.576982, "height": 378, "width": 500, "photo_title": "\u4e0a\u6d77\uff0c\u522b\u5885", "latitude": 31.134288000000002, "owner_url": "http://www.panoramio.com/user/191572", "owner_id": 191572, "photo_file_url": "http://mw2.google.com/mw-panoramio/photos/medium/7565141.jpg", "photo_url": "http://www.panoramio.com/photo/7565141"}, {"upload_date": "13 March 2009", "owner_name": "Baycrest", "photo_id": 20002811, "longitude": 121.483662, "height": 375, "width": 500, "photo_title": "\u8000\u534e\u8def\u957f\u6e05\u8def\u9644\u8fd1 Yaohua Road Changqing Road (2009-01)", "latitude": 31.177959000000001, "owner_url": "http://www.panoramio.com/user/239280", "owner_id": 239280, "photo_file_url": "http://mw2.google.com/mw-panoramio/photos/medium/20002811.jpg", "photo_url": "http://www.panoramio.com/photo/20002811"}, {"upload_date": "17 December 2010", "owner_name": "Faintlightofdawn", "photo_id": 45188287, "longitude": 121.49072200000001, "height": 333, "width": 500, "photo_title": "\u4e2d\u56fd\u9986 - The Crown of the East: Shanghai World Expo 2010", "latitude": 31.185454, "owner_url": "http://www.panoramio.com/user/1235840", "owner_id": 1235840, "photo_file_url": "http://mw2.google.com/mw-panoramio/photos/medium/45188287.jpg", "photo_url": "http://www.panoramio.com/photo/45188287"}, {"upload_date": "11 February 2011", "owner_name": "Faintlightofdawn", "photo_id": 47960081, "longitude": 121.466689, "height": 333, "width": 500, "photo_title": "\u5362\u6e7e\u533a\u9ec4\u660f - Twilight Over Luwan", "latitude": 31.212253, "owner_url": "http://www.panoramio.com/user/1235840", "owner_id": 1235840, "photo_file_url": "http://mw2.google.com/mw-panoramio/photos/medium/47960081.jpg", "photo_url": "http://www.panoramio.com/photo/47960081"}, {"upload_date": "11 February 2011", "owner_name": "Derek\u90dd\u2665\u83ef\u590fCHINA", "photo_id": 47933546, "longitude": 121.483405, "height": 281, "width": 500, "photo_title": "\u4e0a\u6d77\u4f01\u696d\u806f\u5408\u9928\u00b7\u5149\u4e4b\u9b54\u65b9 Shining Cube,Shanghai Corporate Pavilion", "latitude": 31.197357, "owner_url": "http://www.panoramio.com/user/1172446", "owner_id": 1172446, "photo_file_url": "http://mw2.google.com/mw-panoramio/photos/medium/47933546.jpg", "photo_url": "http://www.panoramio.com/photo/47933546"}, {"upload_date": "15 January 2011", "owner_name": "Pozlp\u98ce\u683c", "photo_id": 46564928, "longitude": 121.49243799999999, "height": 500, "width": 361, "photo_title": "\u706f\u5854\u6e29\u5ea6\u8ba1 Lighthouse Thermometer  (21 Celsius  20/10/2010 17:25:38 Shanhai World Expo2010)", "latitude": 31.201495999999999, "owner_url": "http://www.panoramio.com/user/4826506", "owner_id": 4826506, "photo_file_url": "http://mw2.google.com/mw-panoramio/photos/medium/46564928.jpg", "photo_url": "http://www.panoramio.com/photo/46564928"}, {"upload_date": "30 November 2010", "owner_name": "Pozlp\u98ce\u683c", "photo_id": 44398488, "longitude": 121.43652600000001, "height": 333, "width": 500, "photo_title": "\u79cb\u8272\u4e0a\u6d77 Colors of autumn in Shanghai", "latitude": 31.188523, "owner_url": "http://www.panoramio.com/user/4826506", "owner_id": 4826506, "photo_file_url": "http://mw2.google.com/mw-panoramio/photos/medium/44398488.jpg", "photo_url": "http://www.panoramio.com/photo/44398488"}, {"upload_date": "18 September 2010", "owner_name": "Pozlp\u98ce\u683c", "photo_id": 40923211, "longitude": 121.40304600000002, "height": 375, "width": 500, "photo_title": "\u4f59\u6656 Sunset", "latitude": 31.136427999999999, "owner_url": "http://www.panoramio.com/user/4826506", "owner_id": 4826506, "photo_file_url": "http://mw2.google.com/mw-panoramio/photos/medium/40923211.jpg", "photo_url": "http://www.panoramio.com/photo/40923211"}, {"upload_date": "29 September 2010", "owner_name": "GuoJunjun", "photo_id": 41442765, "longitude": 121.487932, "height": 334, "width": 500, "photo_title": "Expo Boulevard \u4e16\u535a\u8f74", "latitude": 31.189235, "owner_url": "http://www.panoramio.com/user/2481456", "owner_id": 2481456, "photo_file_url": "http://mw2.google.com/mw-panoramio/photos/medium/41442765.jpg", "photo_url": "http://www.panoramio.com/photo/41442765"}, {"upload_date": "13 September 2010", "owner_name": "GuoJunjun", "photo_id": 40699575, "longitude": 121.476742, "height": 431, "width": 500, "photo_title": "EXPO: Latvian pavilion \u62c9\u8131\u7ef4\u4e9a\u9986", "latitude": 31.185448999999998, "owner_url": "http://www.panoramio.com/user/2481456", "owner_id": 2481456, "photo_file_url": "http://mw2.google.com/mw-panoramio/photos/medium/40699575.jpg", "photo_url": "http://www.panoramio.com/photo/40699575"}, {"upload_date": "02 April 2011", "owner_name": "Pozlp\u98ce\u683c", "photo_id": 50379651, "longitude": 121.39918599999999, "height": 500, "width": 411, "photo_title": "\u65e0\u9898 Untitled", "latitude": 31.202732999999998, "owner_url": "http://www.panoramio.com/user/4826506", "owner_id": 4826506, "photo_file_url": "http://mw2.google.com/mw-panoramio/photos/medium/50379651.jpg", "photo_url": "http://www.panoramio.com/photo/50379651"}, {"upload_date": "18 September 2010", "owner_name": "GuoJunjun", "photo_id": 40948249, "longitude": 121.488361, "height": 334, "width": 500, "photo_title": "Expo Square \u7eff\u5730\u5e7f\u573a", "latitude": 31.196504000000001, "owner_url": "http://www.panoramio.com/user/2481456", "owner_id": 2481456, "photo_file_url": "http://mw2.google.com/mw-panoramio/photos/medium/40948249.jpg", "photo_url": "http://www.panoramio.com/photo/40948249"}, {"upload_date": "15 October 2009", "owner_name": "Avel-Breizh", "photo_id": 27880019, "longitude": 121.46795500000002, "height": 500, "width": 500, "photo_title": "\u5c0b\u627e\u5a01\u5229 - Where's Wally ? - Dove e Wally ? -  ? \u05d0\u05d9\u05e4\u05d4 \u05d0\u05e4\u05d9  -  Where's Waldo ? - \u00bf D\u00f3nde est\u00e1 Wally ? - Wo ist Walter ? - O\u00f9 est Charlie ?", "latitude": 31.197657, "owner_url": "http://www.panoramio.com/user/407631", "owner_id": 407631, "photo_file_url": "http://mw2.google.com/mw-panoramio/photos/medium/27880019.jpg", "photo_url": "http://www.panoramio.com/photo/27880019"}, {"upload_date": "26 September 2010", "owner_name": "GuoJunjun", "photo_id": 41319252, "longitude": 121.47076099999998, "height": 346, "width": 500, "photo_title": "Angola Pavilion \u5b89\u54e5\u62c9\u9986", "latitude": 31.183301, "owner_url": "http://www.panoramio.com/user/2481456", "owner_id": 2481456, "photo_file_url": "http://mw2.google.com/mw-panoramio/photos/medium/41319252.jpg", "photo_url": "http://www.panoramio.com/photo/41319252"}, {"upload_date": "29 March 2011", "owner_name": "Derek\u90dd\u2665\u83ef\u590fCHINA", "photo_id": 50188909, "longitude": 121.47778299999999, "height": 281, "width": 500, "photo_title": "\u4e16\u535a\u6703\u535a\u7269\u9928 World Exposition Museum", "latitude": 31.196145999999999, "owner_url": "http://www.panoramio.com/user/1172446", "owner_id": 1172446, "photo_file_url": "http://mw2.google.com/mw-panoramio/photos/medium/50188909.jpg", "photo_url": "http://www.panoramio.com/photo/50188909"}, {"upload_date": "11 September 2010", "owner_name": "GuoJunjun", "photo_id": 40615559, "longitude": 121.49321099999997, "height": 373, "width": 500, "photo_title": "EXPO: Japan Pavilion \u65e5\u672c\u9986", "latitude": 31.192043000000002, "owner_url": "http://www.panoramio.com/user/2481456", "owner_id": 2481456, "photo_file_url": "http://mw2.google.com/mw-panoramio/photos/medium/40615559.jpg", "photo_url": "http://www.panoramio.com/photo/40615559"}, {"upload_date": "27 August 2010", "owner_name": "Pozlp\u98ce\u683c", "photo_id": 39900293, "longitude": 121.48427400000003, "height": 248, "width": 500, "photo_title": "Night view of Shanghai World Expo - Huangpu River", "latitude": 31.186748000000001, "owner_url": "http://www.panoramio.com/user/4826506", "owner_id": 4826506, "photo_file_url": "http://mw2.google.com/mw-panoramio/photos/medium/39900293.jpg", "photo_url": "http://www.panoramio.com/photo/39900293"}, {"upload_date": "29 March 2011", "owner_name": "Derek\u90dd\u2665\u83ef\u590fCHINA", "photo_id": 50189567, "longitude": 121.48900500000002, "height": 281, "width": 500, "photo_title": "\u76e7\u6d66\u5927\u6a4b Lupu Bridge", "latitude": 31.193677000000001, "owner_url": "http://www.panoramio.com/user/1172446", "owner_id": 1172446, "photo_file_url": "http://mw2.google.com/mw-panoramio/photos/medium/50189567.jpg", "photo_url": "http://www.panoramio.com/photo/50189567"}, {"upload_date": "21 February 2007", "owner_name": "gumbase", "photo_id": 926722, "longitude": 121.456186, "height": 500, "width": 375, "photo_title": "Calling Girl", "latitude": 31.219792999999999, "owner_url": "http://www.panoramio.com/user/192163", "owner_id": 192163, "photo_file_url": "http://mw2.google.com/mw-panoramio/photos/medium/926722.jpg", "photo_url": "http://www.panoramio.com/photo/926722"}, {"upload_date": "06 January 2011", "owner_name": "Avel-Breizh", "photo_id": 46153292, "longitude": 121.45026300000001, "height": 500, "width": 338, "photo_title": "On a perdu Charlie ----------------> (to enlarge)", "latitude": 31.188279999999999, "owner_url": "http://www.panoramio.com/user/407631", "owner_id": 407631, "photo_file_url": "http://mw2.google.com/mw-panoramio/photos/medium/46153292.jpg", "photo_url": "http://www.panoramio.com/photo/46153292"}, {"upload_date": "21 August 2011", "owner_name": "Dr.Azzouqa", "photo_id": 57722673, "longitude": 121.47353399999997, "height": 375, "width": 500, "photo_title": "A street in the french quarter", "latitude": 31.216076999999999, "owner_url": "http://www.panoramio.com/user/315243", "owner_id": 315243, "photo_file_url": "http://mw2.google.com/mw-panoramio/photos/medium/57722673.jpg", "photo_url": "http://www.panoramio.com/photo/57722673"}, {"upload_date": "21 August 2010", "owner_name": "Hubs56", "photo_id": 39617944, "longitude": 121.502163, "height": 362, "width": 500, "photo_title": "EXPO 2010 - Spanish pavilon", "latitude": 31.202795999999999, "owner_url": "http://www.panoramio.com/user/2679635", "owner_id": 2679635, "photo_file_url": "http://mw2.google.com/mw-panoramio/photos/medium/39617944.jpg", "photo_url": "http://www.panoramio.com/photo/39617944"}, {"upload_date": "18 August 2010", "owner_name": "Hubs56", "photo_id": 39475337, "longitude": 121.50844600000001, "height": 375, "width": 500, "photo_title": "EXPO 2010 - Culture Center _my contest 10-08 ", "latitude": 31.214048999999999, "owner_url": "http://www.panoramio.com/user/2679635", "owner_id": 2679635, "photo_file_url": "http://mw2.google.com/mw-panoramio/photos/medium/39475337.jpg", "photo_url": "http://www.panoramio.com/photo/39475337"}], "has_more": true}
"""
baseurl = Template( "http://www.panoramio.com/map/get_panoramas.php?set=all&"\
        "from=$fr&to=$to&minx=$minx&miny=$miny&maxx=$maxx&maxy=$maxy&mapfilter=false&size=original" )

pageSize = 100
listSize = 10 * pageSize
minx = 121.07
miny = 30.46
maxx = 121.60
maxy = 31.22

jdecoder = simplejson.JSONDecoder();

def getHtml( url ):
        print 'opening %s' % url
        try:
            sock = urlopen( url, timeout = 3 )
            #sock.settimeout(10)
            str = sock.read()
        except IOError:
            str = ""
        except URLError:
            str = ""
        except HTTPError:
            str = ""
        return str

def getObjs( json ):
    objs = jdecoder.decode( json )
    return objs


def getPages( idx ):
    idxrange = range( idx * listSize / pageSize, ( idx + 1 ) * listSize / pageSize )
    list = ''
    for i in idxrange:

        fr = i * pageSize
        to = fr + pageSize

        flag = False
        while not flag:
            try:
                html = getHtml( baseurl.substitute( fr = fr, to = to, minx = minx, miny = miny, maxx = maxx, maxy = maxy ) )
                time.sleep( 1 )
                objs = getObjs( html )
                if objs['has_more'] == False:
                    list += readList( objs )
                    saveList( idx, list )
                    return False
                flag = True
            except simplejson.decoder.JSONDecodeError:
                print 'tired...need to sleep'
                time.sleep( 10 )


        list += readList( objs )
    saveList( idx, list )
    return True

def readList( objs ):
    list = ''
    for img in objs['photos']:
        list += str( img['photo_id'] ) + '\t'
        list += str( img['longitude'] ) + '\t'
        list += str( img['latitude'] ) + '\n'
    return list


def saveList( idx, list ):
    f = open( 'list-' + str( idx ), 'w' )
    f.write( list )
    f.flush()
    f.close()

def getChunk( ranges ):
    for lIdx in ranges:
        if not getPages( lIdx ):
            break




if __name__ == '__main__':
    getChunk( range( 0, 10 ) )

